package view.Prize
{
	/**
	 * 飞行物管理
	 * */
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.EventDispatcher;
	
	import utils.LayerUtils;
	import utils.Vector2D;
	
	public class PrizeManager extends EventDispatcher
	{
		private static var _instance:PrizeManager;
		public function PrizeManager()
		{
			super();
		}
		
		public static function getInstance():PrizeManager{
			return _instance ||= new PrizeManager();
		}
		
		/**舞台上道具存放的数组*/
		private var _prizeVec:Vector.<PrizeView>;
		/**舞台上道具存放的容器*/
		private var _prizeContainer:Sprite;
		
		public function start():void{
			_prizeVec = new Vector.<PrizeView>();
			_prizeContainer = new Sprite();
			_prizeContainer.touchable = false;
			
			_prizeContainer.addEventListener(Event.ENTER_FRAME, movePrize);
			LayerUtils.getInstance().gameLayer.addChild(_prizeContainer);
		}
		
		public function addPrize(id:uint, startX:int, startY:int):void{
//		public function addPrize(id:uint, startX:int, startY:int, _angle:Number):void{
			var prize:PrizeView  = new PrizeView(id);
			_prizeVec.push(prize);
			_prizeContainer.addChild(prize);
			prize.position = new Vector2D(startX,startY);
			prize.velocity.length = prize.speed;
//			prize.velocity.angle = _angle;
		}
		
		private function movePrize(event:Event):void
		{
			for(var i:int = _prizeVec.length-1;i>=0;i--){
				var prize:PrizeView = _prizeVec[i];
				prize.followPath(prize.pathArr, true);
				prize.update();
				
				if(prize.energonVec.length == 0){
					_prizeVec.splice(i,1);
				}
				
				if(isOutOfScreen(prize)){
					_prizeVec.splice(i,1);
					_prizeContainer.removeChild(prize);
					prize.dispose();
					prize = null;
				}
			}
		}
		
		private function isOutOfScreen(prize:PrizeView):Boolean{
			return prize.x - 100> Const.WIDTH || prize.x + 100 <0 || prize.y + 75 <0 || prize.y - 100 > Const.HEIGHT;
		}
		
		public function get prizeVec():Vector.<PrizeView>{
			return _prizeVec;
		}
		
		public function get prizeContainer():Sprite{
			return _prizeContainer;
		}
		
	}
}
